/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import entities.EventType;
import java.util.*;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import persistance.dao.SignumDAO;
import persistance.model.*;

/**
 *
 * @author Bobok
 */
@Singleton
public class JPACasusDAO implements CasusDAOLocal {
    @PersistenceContext(unitName = "Rescriptus-ejbPU")
    private EntityManager em;

    @EJB
    private SignumDAO signumDAO;

    /**
     * @deprecated
     * @warning zwraca wszystko z bazy, uzywac tylko do testów na początku
     * @return lista wszystkich Casusów z bazy
     */
    public List<Casus> getAllCasuses() {
        System.out.println("Begin getAllCasuses()");
        List<Casus> casuses = new ArrayList<Casus>();
        Query db = em.createQuery("select c from Casus c");
        List result = db.getResultList();
        System.out.println("Retrieved "+result.size()+" casuses.");
        for(Object o : result){
            if (o instanceof Casus){
                casuses.add((Casus)o);
            }else{
                throw new Error("Not a casus!"); //TODO: nie rzucać!
            }
        }
        System.out.println("End getAllCasuses()");
        return casuses;
    }

    public void insertCasus(Casus casus) {
        System.out.println("Begin insertCasus("+casus+")");

        casus.setMarker(signumDAO.getByName(casus.getMarker().getName()));
        em.persist(casus);
        em.flush();
        System.out.println("End insertCasus("+casus+")");
    }

    public void updateCasus(Casus casus) {
        System.out.println("Begin updateCasus("+casus+")");
        Query db = em.createQuery("select c from Casus c where c.id = :id");
        db.setParameter("id", casus.getId());
        Casus oldCasus = (Casus)db.getSingleResult();
        oldCasus.setTitle(casus.getTitle());
        oldCasus.setDescription(casus.getDescription());
        oldCasus.setMarker(signumDAO.getByName(casus.getMarker().getName()));
        oldCasus.getTimeAndPlace().setCenterLat(casus.getTimeAndPlace().getCenterLat());
        oldCasus.getTimeAndPlace().setCenterLng(casus.getTimeAndPlace().getCenterLng());
        oldCasus.getTimeAndPlace().setYear(casus.getTimeAndPlace().getYear());
        oldCasus.getTimeAndPlace().setDay(casus.getTimeAndPlace().getDay());
        em.persist(oldCasus);
        em.flush();
        System.out.println("End updateCasus("+casus+")");
    }

    public void deleteCasus(long id) {
        System.out.println("Begin deleteCasus("+id+")");
        Query db = em.createQuery("delete from Casus c where c.id = :id");
        db.setParameter("id", id);
        int result = db.executeUpdate();
        System.out.println(result+" casuses deleted.");
        System.out.println("End deleteCasus("+id+")");
    }

    public void persist(Object o) throws Exception {
        em.persist(o);
    }

    public void remove(Object o) throws Exception {
        em.remove(o);
    }
}
